package com.jsmile.cloud.accountcenter.account.model;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.accountcenter.account.event.member.MemberChangePhoneEvent;
import com.jsmile.cloud.accountcenter.account.event.member.MemberRegisterEvent;
import com.jsmile.cloud.accountcenter.account.repo.MemberSearchRepository;
import com.jsmile.cloud.accountcenter.api.constants.AccountConstants;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberRegister;
import com.jsmile.mail.security.event.DomainEventPublisher;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:38:37
 */
@ApiModel
@Data
@TableName("t_ac_member")
public class Member extends Model<Member> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @ApiModelProperty(value = "会员用户id")
    private Long userId;
    @ApiModelProperty(value = "商户id")
    private Long merchantId;
    @ApiModelProperty(value = "账户id")
    private Long accountId;
    @ApiModelProperty(value = "微信小程序openid")
    private String openId;
    @ApiModelProperty(value = "微信公众号openid")
    private String appOpenId;
    @ApiModelProperty(value = "支付宝用户id")
    private String aliUserId;
    @ApiModelProperty(value = "支付宝支付用户id")
    private String aliPayUserId;
    @ApiModelProperty(value = "昵称")
    private String name;
    @ApiModelProperty(value = "会员编码")
    private String memberCode;
    @ApiModelProperty(value = "店铺id")
    private Long shopId;
    @ApiModelProperty(value = "会员等级")
    private Integer level;
    @ApiModelProperty(value = "手机号")
    private String phone;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;
    @ApiModelProperty(value = "省")
    private String province;
    @ApiModelProperty(value = "市")
    private String city;
    @ApiModelProperty(value = "性别")
    private String gender;
    @ApiModelProperty(value = "头像")
    private String faceLink;
    @ApiModelProperty(value = "租户id")
    private Long tenantId;
    @ApiModelProperty(value = "是否新用户 1是 0否")
    private Integer hasNewUser;
    @ApiModelProperty(value = "是否删除")
    @TableLogic
    private Integer delFlag;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;

    public static Member register(ReqMemberRegister reqMemberRegister, Long accountId, Long shopId) {
        Member member = SpringKit.getBean(MemberSearchRepository.class).findOneByUserIdAndTenantId(reqMemberRegister.getUserId(), reqMemberRegister.getTenantId());
        if (null == member) {
            member = BeanUtil.toBean(reqMemberRegister, Member.class);
            member.setCreateTime(new Date());
            member.setId(IdUtil.getSnowflake(AccountConstants.SNOWFLAKE.workerId, AccountConstants.SNOWFLAKE.datacenterId).nextId());
            DomainEventPublisher.publish(new MemberRegisterEvent(member));
        }
        member.setUpdateTime(new Date());
        member.setAccountId(accountId);
        member.setShopId(shopId);
        member.setProvince(reqMemberRegister.getProvince());
        member.setCity(reqMemberRegister.getCity());
        if (!StrUtil.equals(member.getPhone(), reqMemberRegister.getPhone())) {
            DomainEventPublisher.publish(new MemberChangePhoneEvent(member));
        }
        return member;
    }
}
